package com.jeespring.rocketmq.simple.async;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

/**
 * @description Consumer
 * @author 张朝阳
 * @date 2021/9/14 23:36:22
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group");

        consumer.setNamesrvAddr("192.168.19.10:9876");

        // 消费模式默认负载均衡，MessageModel.BROADCASTING：广播模式
        consumer.setMessageModel(MessageModel.CLUSTERING);

        // Subscribe one more more topics to consume.
        consumer.subscribe("simple-message", "*");
        // Register callback to execute on arrival of messages fetched from brokers.
        consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            System.out.printf("%s 接收消息: %s %n", Thread.currentThread().getName(), msgs);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        });

        //Launch the consumer instance.
        consumer.start();

    }
}

